package com.dingpeikeji.quickdeer.modules.orders.web;

import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.realsche.entity.TRealsche;
import com.dingpeikeji.quickdeer.core.modules.realsche.service.TRealscheService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.base.web.FSysController;
import com.dingpeikeji.quickdeer.modules.realsche.service.FRealscheService;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DemandsReact;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FDemandsService;
import com.dingpeikeji.quickdeer.modules.wx.common.web.OAuth;

@Controller
@RequestMapping(value={"{channelType}/orders","{channelType}/pu/orders"})
public class OrdersController {
	
	@Autowired
	private FRealscheService fRealscheService;
	@Autowired
	private TOrdersService tOrdersService;
	@Autowired
	private FDemandsService demandsService;
	@Autowired
	private TRealscheService tRealscheService;

	/**
	 * 订单列表
	 * @throws IOException 
	 */
	@RequestMapping("orderList")
	public String orderList(@PathVariable String channelType,RedirectAttributes redirectAttributes,TOrders orders, Model model, HttpServletRequest request, HttpServletResponse response) throws IOException{
		String viewPage = MessageFormat.format("modules/orders/{0}OrderList", channelType);
		// 当前订票人
		User user = null;
		if("f".equals(channelType)) {
			user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
			orders.setOrderuserid(user.getId());
			Page<TOrders> page = fRealscheService.findOrderPage(new Page<TOrders>(request, response), orders); 
			redirectAttributes.addFlashAttribute("page",page);
			redirectAttributes.addFlashAttribute("user",user);
			redirectAttributes.addFlashAttribute("orders",orders);
			return "redirect:/f/modifyUserInfo";
		}
		if("wx".equals(channelType)) {
			// 微信的用户身份保存在session中
			user = (User)request.getSession().getAttribute(OAuth.SESSION_PUBUSER);
			orders.setOrderuserid(user.getId());
		}
		
		model.addAttribute("user", user);
		Page<TOrders> page = fRealscheService.findOrderPage(new Page<TOrders>(request, response), orders); 
		page.setPageSize(10);
		model.addAttribute("page", page);
		model.addAttribute("orders", orders);
		return viewPage;
		
	}
	
	/**
	 * 订单详情
	 */
	@RequestMapping("orderDetail")
	public String orderDetail(@PathVariable String channelType, TOrders orders, Model model, HttpServletRequest request, HttpServletResponse response){
		String viewPage = MessageFormat.format("modules/orders/{0}OrderDetail", channelType);
		orders = fRealscheService.getOrders(orders.getOrderid());
		if(orders.getOrdertype().equals(TOrders.ORDER_TYPE_RENTPRODUCT_CUSTOM) && !orders.getOrderstatus().equals(TOrders.STATUS_BACK) && !orders.getOrderstatus().equals(TOrders.STATUS_CANCEL)) {
			TRealsche realsche = tRealscheService.get(orders.getRealscheid());
			if(realsche!=null){
				DemandsReact react = demandsService.getDemandsReact(Integer.parseInt(realsche.getScheid()));
				TDemands tDemands = demandsService.getDemandById(react.getDemandid());
				model.addAttribute("react", react);
				model.addAttribute("tDemands", tDemands);
			}
		}
		model.addAttribute("orders", orders);
		return viewPage;
	}
	
	@RequestMapping("del")
	public String del(@PathVariable String channelType, TOrders order, RedirectAttributes redirectAttributes) {
		String viewPage = MessageFormat.format("redirect:/{0}/orders/orderList", channelType);
		/* TODO 删除逻辑要作处理
		try {
			boolean result = realscheOrderService.delOrder(order);
			if(result) {
				redirectAttributes.addFlashAttribute("message", "删除订单成功");
			} else {
				redirectAttributes.addFlashAttribute("message", "删除订单失败");
			}
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "系统繁忙，稍后再试");
		}*/
		return viewPage;
	}
}
